package other;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Administrator on 2018/5/23.
 */
public class FindRepeat {

    /**
     * 只要找出数组中重复的第一个数字
     */
    public static boolean duplicate(int numbers[],int length,int[] duplication) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++){
            sb.append(numbers[i] + "");
        }
        for(int j = 0; j < length; j++){
            if(sb.indexOf(numbers[j]+"") != sb.lastIndexOf(numbers[j]+"")){
                duplication[0] = numbers[j];
                return true;
            }
        }
        return false;
    }

    /**
     * 只要找出数组中重复的第一个数字
     */
    public static boolean dupHashMap(int numbers[], int length, int[] duplication) {
        boolean flag = false;
        if(numbers==null || length==0){
            return flag;
        }
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int num: numbers){
            if(map.containsKey(num)){
                flag = true;
                duplication[0] = num;
                break;
            }
            map.put(num, 0);
        }
        return flag;
    }

    /**
     * 找出数组中重复的数字，并输出成数组
     */
    public static boolean dupHashMapOutArray(int numbers[], int length,HashMap<Integer,Integer> map) {
        boolean flag = false;
        if(numbers==null || length==0){
            return flag;
        }
//        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int num=0;num<length;num++){
            if(map.containsKey(numbers[num])){
                flag = true;
                map.replace(numbers[num], map.get(numbers[num])+1);
            }else{
                map.put(numbers[num], 1);
            }
        }

        Iterator iter = map.entrySet().iterator();
        while(iter.hasNext()) {
            Map.Entry entry = (Map.Entry)iter.next();
            // 获取key
            Integer key = (Integer)entry.getKey();
            // 获取value
            Integer value= (Integer)entry.getValue();
            if (value>1){
                System.out.println("重复数字："+key+",共有："+value+"个");
            }
        }

        return flag;
    }



    /**
     * 找出数组中重复的数字，并输出成数组
     */
    public static boolean duplicateOutArray(int numbers[],int length,int[] duplication,int[] dupnumber) {
        boolean flag = false;
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++){
            sb.append(Integer.toString(numbers[i]));
//            System.out.println(Integer.toString(numbers[i]));
        }

//        for(int i = 0; i < length; i++){
//            sb.append(numbers[i] + "");
//        }

            for(int j = 0; j < length; j++){
                if(sb.indexOf(Integer.toString(numbers[j])) != sb.lastIndexOf(Integer.toString(numbers[j]))){
                    duplication[dupnumber[0]++] = numbers[j];
                    flag = true;
                }

        }
        return flag;
    }

    public static void main(String[] args){
        int[] arr = {12,44,76,43,36,12,545,43,657,43,5489};
        int[] newarr = new int[arr.length];
        int[] dupnumber = new int[]{0};
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        boolean result = duplicate(arr,arr.length,newarr);

        boolean resultHash = dupHashMap(arr,arr.length,newarr);

        boolean hasSame = duplicateOutArray(arr,arr.length,newarr,dupnumber);

        boolean resultHashOut = dupHashMapOutArray(arr,arr.length,map);

//        Iterator iter = map.entrySet().iterator();
//        while(iter.hasNext()) {
//            Map.Entry entry = (Map.Entry)iter.next();
//            // 获取key
//            Integer key = (Integer)entry.getKey();
//            // 获取value
//            Integer value= (Integer)entry.getValue();
//            if (value>1){
//                System.out.println("重复数字："+key+",共有："+value+"个");
//            }
//        }



    }

}
